package com.lagou.sqlSession;

import com.lagou.pojo.Configuration;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;

/**
 * @author: jie.zhao
 * @create: 2021/01/31 17:05
 */
public class DefaultSqlSession implements SqlSession{

    Executor executor;

    Configuration configuration;

    public DefaultSqlSession(Configuration configuration) {
        this.configuration = configuration;
        executor = new SimpleExecutor();
    }

    @Override
    public <T> List<T> selectList(String statementId, Object... params) throws ClassNotFoundException, SQLException, IllegalAccessException, NoSuchFieldException, InvocationTargetException, IntrospectionException, InstantiationException {
        List<T> resultList = executor.query(statementId,configuration,params);
        return resultList;
    }

    @Override
    public <T> T selectOne(String statementId, Object... params) throws ClassNotFoundException, SQLException, NoSuchFieldException, IllegalAccessException, InstantiationException, IntrospectionException, InvocationTargetException {
        List<T> resultList = selectList(statementId,params);
        if (resultList!=null && resultList.size()==0){
            return resultList.get(0);
        }
        return null;
    }

    @Override
    public <T> T getMapper(Class<T> var) {
        // 创建代理
        return configuration.getMapper(var);
    }
}
